package com.dong.mcd.pub;

import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.dong.mcd.util.JSONUtil;

public class BaseAction  {

	
	/**
	 * 写入json到界面
	 * @param obj
	 * @throws Exception 
	 */
	public void writeJson(HttpServletResponse response,Object obj) throws Exception{
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(JSONUtil.toJson(obj));
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void writeJson(HttpServletResponse response,Collection<?> obj) throws Exception{
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(JSONUtil.arrayToJson(obj));
		response.getWriter().flush();
		response.getWriter().close();  
	}
	
	/**
	 * 写文本到界面
	 * @param text
	 * @throws IOException
	 */
	public void writeText(HttpServletResponse response,String text) throws IOException{
		response.setContentType("text/plain;charset=UTF-8");
		response.getWriter().print(text);
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	/**
	 * 写html到界面
	 * @param content
	 * @throws IOException
	 */
	public void writeHtml(HttpServletResponse response,String content) throws IOException{
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(content);
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	/**
	 * 写xml到界面
	 * @param content
	 * @throws IOException
	 */
	public void writeXml(HttpServletResponse response,String content) throws IOException{
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(content);
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	/**
	 * 写入错误
	 * @param response
	 * @param msg
	 * @throws Exception
	 */
	public void writeError(HttpServletResponse response,Object msg) throws Exception{
		Map<String,Object> error = new HashMap<String, Object>();
		error.put("error", true);
		error.put("result", msg);
		writeJson(response, error);
	}
	
	public void writeSuccess(HttpServletResponse response,Object msg) throws Exception{
		Map<String,Object> error = new HashMap<String, Object>();
		error.put("success", true);
		error.put("result", msg);
		writeJson(response, error);
	}
	
	/**
	 * 返回callback函数
	 * @param response
	 * @param request
	 * @param obj
	 * @throws IOException
	 */
	public void jsonCallback(HttpServletResponse response,HttpServletRequest request,Object obj) throws IOException{
			String callback = request.getParameter("callback");
			if (StringUtils.isNotEmpty(callback)){
				if (obj instanceof Collection )
					writeHtml(response, callback+"("+JSONUtil.arrayToJson(obj)+")");
				else
					writeHtml(response, callback+"("+JSONUtil.toJson(obj)+")");
			}else{
				writeHtml(response,"callback is undefined!");
			}
	}
	
	/**
	 * 成功回调，跨域调用
	 * @param response
	 * @param request
	 * @param obj
	 * @throws Exception
	 */
	public void callSuccess(HttpServletResponse response,HttpServletRequest request,Object obj) throws Exception{
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("success", true);
		map.put("result", obj);
		jsonCallback(response, request, obj);
	}
	
	/**
	 * 失败回调，跨域调用
	 * @param response
	 * @param request
	 * @param obj
	 * @throws Exception
	 */
	public void callError(HttpServletResponse response,HttpServletRequest request,Object obj) throws Exception{
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("error", true);
		map.put("result", obj);
		jsonCallback(response, request, obj);
	}
	
	/**
	 * 得到参数map
	 * @param req
	 * @return
	 */
	public Map<String,String> paramMap(HttpServletRequest req){
		Map<String,String> params = new HashMap<String, String>();
		Enumeration<?> _enum = req.getParameterNames();
		while(_enum.hasMoreElements()){
			String _name = _enum.nextElement().toString();
			params.put(_name, req.getParameter(_name));
		}
		return params;
	}
	
	/**
	 * 得到参数
	 * @param name
	 * @param req
	 * @return
	 */
	public String getParam(String name,HttpServletRequest req){
			return req.getParameter(name);
	}
	
}
